package rpt.yto.flex.remote.data.dao.processor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import rpt.yto.flex.remote.data.bean.DeptdocBean;
import rpt.yto.flex.remote.data.biz.QueryBiz;
import rpt.yto.flex.remote.data.dao.itf.IPubObject;
import rpt.yto.util.ConvertFunc;

@SuppressWarnings({"unchecked"})
public class Query_headpsn {
	
	private Map<String , String> distinctionMap = null;
	
	private Map<String , String> resultMap;

	private static Query_headpsn querySqlFunc = new Query_headpsn();
	
	private Query_headpsn() {
		
	}
	
	public static final Query_headpsn getInstance() {
		return querySqlFunc;
	}
	
	
	
	/**
	 * 
	 * 
	 * @param period
	 * @return
	 * @throws Exception
	 */
	public final StringBuilder getQueryTypeByMap(String period ) throws Exception {
		
		if(distinctionMap == null)
			initDistinctionMap();
		
		StringBuilder builderTypeAppend2 = new StringBuilder();
		
		Iterator iter = distinctionMap.entrySet().iterator();
		while(iter.hasNext()) {
			
			Entry entry = (Entry) iter.next();
			
			String pk_deptdoc = (String) entry.getKey();
			
			String[] vals = ConvertFunc.getInstance().change(entry.getValue()).split("~");
			
			Integer rowno = Integer.valueOf(vals[0]);
			
			String statistictype = vals[1];
			
			StringBuilder builderTypeByMap = new StringBuilder();
			
			builderTypeByMap.append(" select "+rowno+" rowno , '"+statistictype+"' statistictype , sum(endcount) endcount , sum(endcount_w) endcount_w ,  ");
			builderTypeByMap.append(" sum(comcount) comcount , sum(mngcount) mngcount , sum(procount) procount , sum(procount_acc) procount_acc ,  ");
			builderTypeByMap.append(" sum(skillcount) skillcount , sum(skillcount_acc) skillcount_acc , sum(expcount) expcount , sum(expcount_acc) expcount_acc ,  ");
			builderTypeByMap.append(" sum(roundcount) roundcount , sum(roundcount_acc) roundcount_acc , sum(cscount) cscount from ( ");
			// 期末从业人员数
			builderTypeByMap.append(" select count(1) endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount "); 
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 期末女性从业人员数
			builderTypeByMap.append(" select 0 endcount , count(1) endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_psnbasdoc.sex = '女' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 主管以上管理人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , count(1) comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%主管' or om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁' or om_job.jobname like '%高级专员'  or  om_job.jobname like '%专家') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 经理级以上人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , count(1) mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁'  or  om_job.jobname like '%专家') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 专业技术人员数
			// procount
			// 取得技术职称人员数
			// procount_acc
			// 技能人员数 
			// skillcount 
			// 取得技能资格证人员数
			// skillcount_acc
			// 快递业务人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , count(1) expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"%') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%业务员') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 取得业务员资格证人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , count(1) expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%业务员') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 分拣操作人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , count(1) roundcount , 0 roundcount_acc , 0 cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '操作%') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 取得操作员资格证人员
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , count(1) roundcount_acc , 0 cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '操作%') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" union all  ");
			// 客服人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , count(1) cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = 'ORGCORP00000000A0015' ");
			builderTypeByMap.append(JoinPk_Deptdoc(pk_deptdoc));
			builderTypeByMap.append(" and (om_job.jobname like '%客%') ) ");
			
			builderTypeAppend2.append(builderTypeByMap).append(" union all ");
			
			rowno ++;
			
		}
		
		builderTypeAppend2 = new StringBuilder(builderTypeAppend2.substring(0 , builderTypeAppend2.length() - " union all ".length()));
		
		return builderTypeAppend2;
	}
	
	private final Map<String , String> initDistinctionMap() throws Exception {
		
		distinctionMap = new ConcurrentHashMap<String , String>();
		
		List<Map<String , Object>> deptList = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery(" select pk_deptdoc , deptname from bd_deptdoc where pk_corp in (select pk_corp from bd_corp where  bd_corp.unitdistinction = '"+IPubObject.GROUPCORP+"')  and pk_fathedept is null order by pk_deptdoc ", DeptdocBean.class);
		
		Integer rowno = 1;
		for(Map<String , Object> dept : deptList) {
			distinctionMap.put(String.valueOf(dept.get("pk_deptdoc")) , rowno + "~" + dept.get("deptname"));
			rowno ++;
		}
		
		return distinctionMap;
	}
	
	private final String JoinPk_Deptdoc(String superPk_deptdoc) throws Exception {
		
		if(resultMap == null)
			resultMap = new ConcurrentHashMap<String , String>();
		
		if(resultMap.get(superPk_deptdoc) == null)
			resultMap.put(superPk_deptdoc, " and bd_psndoc.pk_deptdoc in ("+unionPk_Deptdoc(new StringBuilder("'" + superPk_deptdoc + "',") , "'" + superPk_deptdoc + "'")+") ");
		
		return resultMap.get(superPk_deptdoc);
	}
	
	private final String unionPk_Deptdoc(StringBuilder builderPk_Deptdoc , String superPk_deptdoc) throws Exception {
		
		String queryString = "select to_char(wmsys.wm_concat(chr(39) || pk_deptdoc || chr(39))) pk_deptdoc from bd_deptdoc where pk_fathedept in ("+superPk_deptdoc+") ";
		List<Map<String , Object>> pk_deptdoc = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery(queryString , DeptdocBean.class);
		
		if(pk_deptdoc == null)
			return superPk_deptdoc;
		
		List<Map<String , Object>> count = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery("select to_char(nvl(count(1) , 0)) pk_deptdoc from bd_deptdoc where pk_fathedept in ("+superPk_deptdoc+") ", DeptdocBean.class);
		
		if(Integer.valueOf(String.valueOf(count.get(0).get("pk_deptdoc"))) > 0) {
			builderPk_Deptdoc.append(pk_deptdoc.get(0).get("pk_deptdoc") + ",");
			unionPk_Deptdoc(builderPk_Deptdoc , String.valueOf(pk_deptdoc.get(0).get("pk_deptdoc")));
		}
		
		if(builderPk_Deptdoc.length() > 0)
			return builderPk_Deptdoc.substring(0 , builderPk_Deptdoc.length() - 1);
		
		return builderPk_Deptdoc.toString();
		
	}
	
	public final String getQuerySQL(String period) throws Exception {
		
		return Query_headpsn.getInstance().getQueryTypeByMap(period).toString();
		
	}

}
